package za.ac.nmmu.muhelp.data;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.logging.Logger;

import za.ac.nmmu.muhelp.model.XMPPConfiguration;

@ApplicationScoped
public class XMPPConfigurationRepository {

    @Inject
    private EntityManager em;

    @Inject
    private Logger log;

    public List<XMPPConfiguration> findAllLive() {
	String query = 
		"select c from za.ac.nmmu.muhelp.model.XMPPConfiguration as c where live = 't'";
	log.info("query is [" + query + "]");
	List<XMPPConfiguration> xmpps = (List<XMPPConfiguration>) em.createQuery(query).getResultList();
	return xmpps;
    }

    public XMPPConfiguration findLiveByUsername(String username) {
	String query = 
		"select c from za.ac.nmmu.muhelp.model.XMPPConfiguration as c where live = 't' and username = '" 
			+ username + "'";
	log.info("query is [" + query + "]");
	List<XMPPConfiguration> xmpps = (List<XMPPConfiguration>) em.createQuery(query).getResultList();
	if ( xmpps != null && xmpps.size() > 0)
		return xmpps.get(0);
	else
		return null;
    }

    
}
